/**
 * Created by guojanyu on 2015/9/12.
 */
myApp.config(function ($routeProvider) {
    $routeProvider.when('/hotBusiness/view', {
        templateUrl: 'hotBusiness/view',
        controller: 'hotBusinessListController'
    })
});
myApp.service('hotBusinessListService', function ($http) {
    var doRequest = function () {
        return $http.get('hotBusiness/list');
    }
    return {
        hotBusinessList: function () {
            return doRequest();
        }
    };
});
myApp.controller('hotBusinessListController', function ($scope, $location, $http,hotBusinessListService,Notify) {
    hotBusinessListService.hotBusinessList().success(function (response) {
        $scope.hotBusinesses = response;
    });
    /*删除商家*/
    $scope.deleteHotBusiness = function (id,name) {
        if (confirm('确定要删除"' + name + '"吗?')) {
            $http.get('hotBusiness/delete/' + id).success(function () {
                Notify.success('删除成功');
                hotBusinessListService.hotBusinessList().success(function (response) {
                    $scope.hotBusinesses = response;
                });
            }).error(function () {
                Notify.error('删除失败');
            });
        }
    };
    /*增加商家*/
    $scope.add=function(){
        $scope.hotBusiness={};
    };
    $scope.addHotBusiness = function () {
        $http.post('hotBusiness/add',$scope.hotBusiness).success(function () {
            Notify.success("保存商家成功");
            hotBusinessListService.hotBusinessList().success(function (response) {
                $scope.hotBusinesses = response;
            });
        }).error(function () {
            Notify.error('失败');
        });
    };
    /*更新商家*/
    $scope.update=function(data){
        $scope.row=angular.copy(data);
    };
    $scope.updateHotBusiness = function () {
        $scope.hotBusiness=angular.copy($scope.row);
        $http.post('hotBusiness/update', $scope.hotBusiness).success(function () {
            Notify.success("更新商家成功");
            hotBusinessListService.hotBusinessList().success(function (response) {
                $scope.hotBusinesses = response;
            });
        }).error(function () {
            Notify.error('失败');
        });
    };
});

